package com.harmontronics.hanma.paas.paas.service;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.harmontronics.hanma.paas.paas.common.MosResponse;
import com.harmontronics.hanma.paas.paas.domain.iam.entity.GroupManageUser;
import com.harmontronics.hanma.paas.paas.domain.iam.entity.valueobject.HmUserGroupManageFields;
import com.harmontronics.hanma.paas.paas.domain.iam.repository.persistence.HmUserGroupManageRepository;
import com.harmontronics.hanma.paas.paas.domain.iam.repository.po.HmUserGroupManagePO;
import com.harmontronics.hanma.paas.paas.domain.iam.service.HmUserGroupManageService;
import com.harmontronics.hanma.paas.paas.domain.organization.entity.SsoUserDtoResult;
import com.harmontronics.hanma.paas.paas.domain.organization.service.MosService;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Service;

@Service("hmUserGroupManageMosService")
@DS("#header.App-Unique")
@ConditionalOnProperty(prefix = "mos.oss",name="enabled",havingValue = "true")
public class HmUserGroupManageMosService extends HmUserGroupManageService implements IHmUserGroupManageService {

    @Autowired
    private HmUserGroupManageRepository hmUserGroupManageRepository;

    @Autowired
    private MosService mosService;

    @Override
    public List<GroupManageUser> selectGroupManageUserWithId(Long groupId){

        List<GroupManageUser> manageUserList = new ArrayList<>();
        QueryWrapper<HmUserGroupManagePO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(HmUserGroupManageFields.USER_GROUP_ID,groupId);
        List<HmUserGroupManagePO> groupManagePOList = hmUserGroupManageRepository.list(queryWrapper);
        List<Long> userIdList = groupManagePOList.stream().map(manager -> manager.getUserId())
                .collect(Collectors.toList());
        if(CollectionUtils.isNotEmpty(userIdList)){
            String tmpIds = userIdList.stream().map(id -> id.toString())
                    .collect(Collectors.joining(","));
            MosResponse<List<SsoUserDtoResult>> response = mosService.listByIds(tmpIds);
            if(response.isOk()){
                List<SsoUserDtoResult> userDtoResultList = response.getData();
                if(CollectionUtils.isNotEmpty(userDtoResultList)){
                    for(SsoUserDtoResult userDtoResult : userDtoResultList){
                        Long userId = userDtoResult.getId();
                        String username = userDtoResult.getUsername();
                        GroupManageUser groupManageUser = new GroupManageUser();
                        if(StringUtils.isNotBlank(username)){
                            username = userDtoResult.getName();
                        }
                        groupManageUser.setId(groupId).setUserId(userId).setName(username);
                        manageUserList.add(groupManageUser);
                    }
                    return manageUserList;
                }
            }
        }
        return manageUserList;
    }


}
